package com.fitbet.admin.handler.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.fitbet.admin.handler.SmsHandler;
import com.fitbet.admin.rao.SmsRao;
/**
 * 
 * @author sjh
 *
 */
@Component("smsHandler")
public class SmsHandlerImpl implements SmsHandler{
	
	private static final Logger logger = LoggerFactory.getLogger(SmsHandlerImpl.class);

	@Value("#{config['sms.host']}")
	private String host;
	
	@Value("#{config['sms.mock']}")
	private boolean mock;
	
	@Rao
	private SmsRao smsRao;
	
	/*
	 * (non-Javadoc)
	 * @see com.fitbet.admin.handler.SmsHandler#sendMessage(java.lang.String, java.lang.String)
	 */
	public boolean sendMessage(String mobile, String content) {
		
		HttpResult httpResult = new HttpResult();
		
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		
		parameters.put("message", content);
		
		parameters.put("mobiles", mobile);
		
		parameters.put("time", 0);
		
		logger.info("start call sms with message [{}], mobiles [{}]", parameters.get("message"), parameters.get("mobile"));
		
		String sendUrl = host+"/send-sms";
		
		logger.info("url is [{}]", sendUrl);
		logger.info("pramas is [{}]", JsonUtils.toJson(parameters));
		
		httpResult = mock ? httpResult.success().message("发送成功") : smsRao.smsSend(sendUrl, parameters);
		
		logger.info("sendMessage get result [{}]", JsonUtils.toJson(httpResult));
		
		return httpResult.isValid();
	}

}
